package com.my.mall.manage.evaluate.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.my.mall.manage.evaluate.dao.EvaluateManageDao;
import com.my.mall.manage.evaluate.dto.EvaluateManageDTO;
import com.my.mall.manage.evaluate.vo.EvaluateManageVo;
import com.my.mall.manage.goods.service.GoodsManageService;
import com.my.mall.manage.goods.vo.info.GoodsInfoVo;
import com.my.mall.manage.user.service.UserManageService;
import com.my.mall.manage.user.vo.UserInfoVo;

@Service
public class EvaluateManageService {
	@Autowired
	EvaluateManageDao evaluateManageDao;

	@Autowired
	UserManageService userManageService;

	@Autowired
	GoodsManageService goodsManageService;

	public EvaluateManageVo getEvaluate(int id) {
		return evaluateManageDao.getEvaluate(id);
	}

	public int selectEvaluateCount(EvaluateManageDTO evaluate) {

		if ("".equals(evaluate.getGoodsName())) {
			evaluate.setGoodsId(0);
		} else {
			GoodsInfoVo goods = goodsManageService.getInfoByGoodsName(evaluate.getGoodsName());
			if (goods != null) {
				evaluate.setGoodsId(goods.getId());
			} else {
				evaluate.setGoodsId(-1);
			}
		}

		if ("".equals(evaluate.getUserName())) {
			evaluate.setUserId(0);
		} else {
			UserInfoVo user = userManageService.getUserByUserName(evaluate.getUserName());
			if (user != null) {
				evaluate.setUserId(user.getId());
			} else {
				evaluate.setUserId(-1);
			}
		}
		return evaluateManageDao.selectEvaluateCountByParameter(evaluate.getGoodsId(), evaluate.getUserId(),
				evaluate.getStar(), evaluate.getIsUseful());
	}

	public List<EvaluateManageVo> selectEvaluateList(EvaluateManageDTO evaluate, int iDisplayStart,
			int iDisplayLength) {
		if ("".equals(evaluate.getGoodsName())) {
			evaluate.setGoodsId(0);
		} else {
			GoodsInfoVo goods = goodsManageService.getInfoByGoodsName(evaluate.getGoodsName());
			if (goods != null) {
				evaluate.setGoodsId(goods.getId());
			} else {
				evaluate.setGoodsId(-1);
			}
		}

		if ("".equals(evaluate.getUserName())) {
			evaluate.setUserId(0);
		} else {
			UserInfoVo user = userManageService.getUserByUserName(evaluate.getUserName());
			if (user != null) {
				evaluate.setUserId(user.getId());
			} else {
				evaluate.setUserId(-1);
			}
		}
		List<EvaluateManageVo> list = evaluateManageDao.selectEvaluateListByParameter(evaluate.getGoodsId(),
				evaluate.getUserId(), evaluate.getStar(), evaluate.getIsUseful(), iDisplayStart, iDisplayLength);
		for (EvaluateManageVo emo : list) {
			GoodsInfoVo giv = goodsManageService.getInfo(emo.getId());
			if (giv != null) {
				emo.setGoodsName(giv.getName());
			}
			UserInfoVo umv = userManageService.getUser(emo.getId());
			if (umv != null) {
				emo.setUserName(umv.getUserName());
			}
		}
		return list;
	}

	public int updateEvaluateReply(EvaluateManageDTO evaluate) {
		return evaluateManageDao.updateEvaluateReply(evaluate.getId(), evaluate.getStar(), evaluate.getIsUseful(),
				evaluate.getReply());
	}

}